<script setup>
import { nextTick, onMounted, onUnmounted, ref, watch } from 'vue'
import Card from '@/views/new-screen/components/card.vue'
import DrawEcharts from '@/components/draw-echarts.vue'
import { getDemonstrationBelt, getDemonstrationSite } from '../api.js'

const props = defineProps({
  isExpanded: Boolean,
  activeName: String,
})
const getBarChartOption = (data, title) => {
  const barColors = {
    '示范带': {
      type: 'linear',
      x: 0,
      y: 0,
      x2: 0,
      y2: 1,
      colorStops: [{
          offset: 0, color: '#D9D8FF' // 0% 处的颜色
      }, {
          offset: 1, color: '#8184FF' // 100% 处的颜色
      }],
      global: false // 缺省为 false
    },
    '市级示范点': {
      type: 'linear',
      x: 0,
      y: 0,
      x2: 0,
      y2: 1,
      colorStops: [{
          offset: 0, color: '#FFE576' // 0% 处的颜色
      }, {
          offset: 1, color: '#FF8F3B' // 100% 处的颜色
      }],
      global: false // 缺省为 false
    },
    '县级示范点': {
      type: 'linear',
      x: 0,
      y: 0,
      x2: 0,
      y2: 1,
      colorStops: [{
          offset: 0, color: '#B7FFEF' // 0% 处的颜色
      }, {
          offset: 1, color: '#10FEBE' // 100% 处的颜色
      }],
      global: false // 缺省为 false
    }
  }
  return {
    grid: {
      top: '8%',
      right: '3%',
      left: '3%',
      bottom: '15%',
      // containLabel: true
    },
    legend: {
      show: false,
      right: 'center',
      itemWidth: 6,
      itemHeight: 6,
      textStyle: {
        color: '#b4d3ed',
        fontSize: 12,
      },
    },
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'shadow'
      }
    },
    xAxis: {
      data: data.xAxis,
      axisLabel: {
        showMaxLabel: true,
        textStyle: {
          color: '#ffffff',
          fontSize: 9,
        },
        rotate: -45,
        padding: [3, 0, 0, 0],
      },
      axisLine: {
        show: true,
        lineStyle: {
          color: 'rgb(64,77,93)',
          width: 1,
        },
      },
      axisTick: {
        show: false,
        lineStyle: {
          color: 'rgb(64,77,93)',
        },
      },
      splitLine: {
        show: false,
      },
    },
    yAxis: [
      {
        name: '数量',
        type: 'value',
        position: 'left',
        axisLabel: {
          show: false,
          formatter: '{value}',
          textStyle: {
            color: '#b4d3ed',
            fontSize: 12,
          },
        },
        axisLine: {
          show: false,
          lineStyle: {
            color: '#b4d3ed',
            width: 1,
          },
        },
        splitLine: {
          show: true,
        },
      },
    ],
    series: [
      {
        name: title,
        type: 'bar',
        barWidth: 10,
        showBackground: false,
        label: {
          // 数据标签配置
          show: true, // 显示标签
          position: 'top', // 标签位置（top/inside/outside等）
          formatter: '{c}', // 显示数据值（{c} 代表数据值）
          fontSize: 10,
          color: '#fff'
        },
        itemStyle: {
          color: barColors[title] //
          // {
          //   type: 'linear',
          //   x: 0,
          //   y: 0,
          //   x2: 0,
          //   y2: 1,
          //   colorStops: [{
          //       offset: 0, color: '#3df389' // 0% 处的颜色
          //   }, {
          //       offset: 1, color: '#0cb6e7' // 100% 处的颜色
          //   }],
          //   global: false // 缺省为 false
          // }
        },
        data: data.data,
      },
    ],
    backgroundColor: {
      type: 'linear',
      x: 0,
      y: 0,
      x2: 1,
      y2: 0,
      colorStops: [{
          offset: 0, color: '#01153c89' // 0% 处的颜色
      }, {
          offset: 1, color: '#01153c21' // 100% 处的颜色
      }],
      global: false // 缺省为 false
    }
  }
}

const demoZoneChartBarRef = ref(null)
const cityDemoPointChartBarRef = ref(null)
const countyDemoPointChartBarRef = ref(null)

const updateDataSite = (res) => {
  cityDemoPointChartBarRef.value?.updateOption(getBarChartOption({
    xAxis: res.shiqu.cityNames,
    data: res.shiqu.value
  }, '市级示范点'))
  countyDemoPointChartBarRef.value?.updateOption(getBarChartOption({
    xAxis: res['县级示范点'].cityNames,
    data: res['县级示范点'].value
  }, '县级示范点'))
}

const updateDataBelt = (res) => {
  const data = {
    xAxis: res.cityNames,
    data: res.value
  }
  demoZoneChartBarRef.value?.updateOption(getBarChartOption(data, '示范带'))
}

const reqData = () => {
  Promise.all([
    getDemonstrationBelt(),
    getDemonstrationSite()
  ])
  .then(([belt, site]) => {
    if (belt.success) {
      updateDataBelt(belt.data)
    }
    if (site.success) {
      updateDataSite(site.data)
    }
  })
}

onMounted(() => {
  reqData()
})
</script>

<template>
  <div :class="['left', isExpanded ? 'active' : '']">
    <card name="示范带" class="township-card">
      <div class="chart">
        <draw-echarts
          ref="demoZoneChartBarRef"
          width="420"
          height="240"
          :options="{}"
        ></draw-echarts>
      </div>
    </card>

    <card name="示范点" class="township-card">
      <div class="section">
        <div class="section-title">＞ 市级示范点</div>
        <div class="chart">
          <draw-echarts
            ref="cityDemoPointChartBarRef"
            width="420"
            height="240"
            :options="{}"
          ></draw-echarts>
        </div>
        <div class="section-title mt-2">＞ 县级示范点</div>
        <div class="chart">
          <draw-echarts
            ref="countyDemoPointChartBarRef"
            width="420"
            height="240"
            :options="{}"
          ></draw-echarts>
        </div>
      </div>
    </card>
  </div>
</template>

<style scoped lang="scss">
.left {
  .title-icon {
    display: inline-flex;
    background-repeat: no-repeat;
    background-size: 100% 100%;
    height: 61px;
    position: relative;
    bottom: 20px;
    // margin-top: 10px;
  }
  .mt-2 {
    margin-top: 16px;
  }
  .section-title {
    line-height: 30px;
    font-size: 16px;
    font-weight: bold;
    color: #fff;
    margin-bottom: 10px;
  }
}
</style>
